package com.corpgovernment.travelstandard.impl;

import com.corpgovernment.common.currency.CurrencyConversionUtil;
import com.corpgovernment.common.currency.bo.CurrencyExchangeBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.Optional;

/***
 * @author zwmao
 * @version 1.0.0
 * @date 2023/9/7
 **/
@Service
@Slf4j
public class MbTravelstandCurrencyExchangeService {
    /**
     * 默认币种符号为人民币
     */
    private static final String CNY_CODE = "CNY";

    /**
     * 人民币符号
     */
    private static final Character CNY_SYMBOL = '¥';

    public String showContext(String currencyCode, BigDecimal priceUpperLimit) {
        if (CNY_CODE.equals(currencyCode)) {
            return CNY_SYMBOL + priceUpperLimit.setScale(0, RoundingMode.HALF_UP).toString() + "/间夜及以下";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(currencyCode).append(" ").append(priceUpperLimit.setScale(0, RoundingMode.HALF_UP).toString()).append("/间夜及以下");
        CurrencyExchangeBO currencyExchangeBO = CurrencyConversionUtil.exchangeCurrency(currencyCode, CNY_CODE, priceUpperLimit);
        if (!Objects.isNull(currencyExchangeBO)) {
            sb.append("(等价人民币为").append(CNY_SYMBOL).append(currencyExchangeBO.getToCurrencyAmount().setScale(0, RoundingMode.HALF_UP).toString()).append("/间夜)");
            log.info("汇率转换前币种为{}，金额为{}，转换后未四舍五入的金额为¥{}，当前token为{}",currencyCode,priceUpperLimit.toString(),currencyExchangeBO.getToCurrencyAmount().toString(),currencyExchangeBO.getExchangeToken());
        }
        return sb.toString();
    }

    public CurrencyExchangeBO exchangeAmountAndToken(String currencyCode, BigDecimal priceUpperLimit) {
        CurrencyExchangeBO exchangeBOResp = new CurrencyExchangeBO();
        //如果无法获取限价，则认为不限制
        if (Objects.isNull(priceUpperLimit)){
            exchangeBOResp.setToCurrencyAmount(new BigDecimal(Long.MAX_VALUE));
            exchangeBOResp.setExchangeToken(null);
            return exchangeBOResp;
        }
        if (CNY_CODE.equals(currencyCode)) {
            exchangeBOResp.setToCurrencyAmount(Optional.ofNullable(priceUpperLimit).orElse(BigDecimal.ZERO));
            exchangeBOResp.setExchangeToken(null);
            return exchangeBOResp;
        }
        CurrencyExchangeBO currencyExchangeBO = CurrencyConversionUtil.exchangeCurrency(currencyCode, CNY_CODE, priceUpperLimit);
        if (!Objects.isNull(currencyExchangeBO)) {
            exchangeBOResp.setToCurrencyAmount(Optional.ofNullable(currencyExchangeBO.getToCurrencyAmount()).orElse(BigDecimal.ZERO));
            exchangeBOResp.setExchangeToken(currencyExchangeBO.getExchangeToken());
            log.info("汇率转换前币种为{}，金额为{}，转换后未四舍五入的金额为¥{}，当前token为{}",currencyCode,priceUpperLimit.toString(),currencyExchangeBO.getToCurrencyAmount().toString(),currencyExchangeBO.getExchangeToken());
        } else {
            // 当汇率转换出错时，将差标设为0，此时管控所有酒店
            exchangeBOResp.setToCurrencyAmount(BigDecimal.ZERO);
            exchangeBOResp.setExchangeToken(null);
        }
        return exchangeBOResp;
    }

    /**
     * 机票金额管控
     * @param currencyCode
     * @param amountNum
     * @return
     */
    public String showFlightAmountContext(String currencyCode, Integer amountNum) {
        if (CNY_CODE.equals(currencyCode)) {
            return "金额管控价格上限" + CNY_SYMBOL + amountNum;
        }
        StringBuilder sb = new StringBuilder();
        sb.append("金额管控价格上限").append(currencyCode).append(" ").append(amountNum);
        CurrencyExchangeBO currencyExchangeBO = CurrencyConversionUtil.exchangeCurrency(currencyCode, CNY_CODE, new BigDecimal(amountNum));
        if (!Objects.isNull(currencyExchangeBO)) {
            sb.append("(等价人民币为").append(CNY_SYMBOL).append(currencyExchangeBO.getToCurrencyAmount().toString()).append(")");
            log.info("汇率转换前币种为{}，金额为{}，转换后未四舍五入的金额为¥{}，当前token为{}", currencyCode, amountNum,
                    currencyExchangeBO.getToCurrencyAmount().toString(), currencyExchangeBO.getExchangeToken());
        }
        return sb.toString();
    }

}
